import { getDefaultFields } from '../utils';

module.exports = app => {
	const DataTypes = app.Sequelize;

	const Model = app.model.define('market', {
		id: {
			type: DataTypes.INTEGER(8).UNSIGNED,
			allowNull: false,
			primaryKey: true,
			autoIncrement: true
		},
		name: {
			type: DataTypes.STRING(255),
			allowNull: false,
			defaultValue: ''
		},
		type: {
			type: DataTypes.STRING(10),
			allowNull: false,
			enum: ['SCROLL', 'ONE', 'TWO'],
			defaultValue: ''
		},
		status: {
			type: DataTypes.INTEGER(2).UNSIGNED,
			allowNull: true,
			defaultValue: 0,
			comment: '0-> 停用，1-> 启用'
		},
		sort: {
			type: DataTypes.INTEGER.UNSIGNED,
			allowNull: true,
			defaultValue: 0,
			comment: '排序'
		},
		...getDefaultFields()
	}, {
		tableName: 'market',
		timestamps: false
	});

	Model.sync({ alter: true });

	Model.associate = function () {
		app.model.Market.belongsToMany(app.model.Goods, {
			through: app.model.MarketGoods,
			foreignKey: 'market_id',
			otherKey: 'goods_id'
		});
	}
	return Model;
};
